package cn.rayland.rayland3d.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;

import cn.rayland.rayland3d.R;

/**
 * Created by wuyaxian on 2016/5/3.
 */
public class PushWebSocketActivity extends AppCompatActivity {
    Switch pushSwitch;
    EditText address;
    EditText port;
    EditText interval;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pushwebsocket);
        initView();
        address.setText(EasySharePreference.getPrefInstance(this).getString("pushAddress", ""));
        port.setText(EasySharePreference.getPrefInstance(this).getInt("pushPort", 0) + "");
        interval.setText((EasySharePreference.getPrefInstance(this).getInt("pushTime", 5000) / 1000) + "");
        pushSwitch.setChecked(EasySharePreference.getPrefInstance(this).getBoolean("ifpush", false));

        pushSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                EasySharePreference.getEditorInstance(getApplicationContext()).putBoolean("ifpush", b).commit();
            }
        });
    }

    private void initView() {
        pushSwitch = (Switch) findViewById(R.id.ifpush);
        address = (EditText) findViewById(R.id.address);
        port = (EditText) findViewById(R.id.port);
        interval = (EditText) findViewById(R.id.interval);
    }

    @Override
    protected void onPause() {
        super.onPause();
        EasySharePreference.getEditorInstance(this).putString("pushAddress", address.getText().toString().trim()).commit();
        EasySharePreference.getEditorInstance(this).putInt("pushPort", Integer.parseInt(port.getText().toString().trim())).commit();
        EasySharePreference.getEditorInstance(this).putInt("pushTime", Integer.parseInt(interval.getText().toString().trim()) * 1000).commit();
    }
}
